package com.room.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyh.dao.dto.RoomDto;
import com.cyh.dao.dto.SeatDto;
import com.cyh.dao.entity.Room;
import com.cyh.dao.entity.SysUser;
import com.room.core.annotation.LoginUser;
import com.room.core.dto.UserInfo;
import com.room.core.util.R;
import com.room.service.RoomService;
import com.room.vo.ReserveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    @PostMapping("buildList")
    public List<Room> roomList(@LoginUser UserInfo userInfo) {
        return roomService.list(new QueryWrapper<Room>().eq("level", 0));
    }

    @PostMapping("parentIdList/{parentId}")
    public List<RoomDto> roomList(@PathVariable("parentId") Long parentId,
            @RequestParam("dmtIs") Integer dmtIs) {//0自习室， 1多媒体
        return roomService.roomListByBuildId(parentId,dmtIs);
    }

    @PostMapping("seatList/{parentId}")
    public List<Map<String, Object>> seatList(@PathVariable("parentId") Long parentId) {
        return roomService.seatListByRoomId(parentId);
    }

    /**
     * 预约
     *
     * @return
     */
    @PostMapping("reserve")
    public R reserve(@LoginUser UserInfo userInfo, @RequestBody ReserveVo reserveVo) {
        return roomService.reserve(userInfo, reserveVo);
    }

    @PostMapping("autoReserve")
    public R autoReserve(@LoginUser UserInfo userInfo, @RequestBody ReserveVo reserveVo) {
        return roomService.autoReserve(userInfo, reserveVo);
    }


    /**
     * 文件上传具体实现方法;
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                              @RequestParam("seatId") Integer seatId,
                              @RequestParam("timeInterval")Integer timeInterval,
                              @LoginUser UserInfo userInfo) {
        if (!file.isEmpty()) {
            try {
                /*
                 * 这段代码执行完毕之后，图片上传到了工程的跟路径； 大家自己扩散下思维，如果我们想把图片上传到
                 * d:/files大家是否能实现呢？ 等等;
                 * 这里只是简单一个例子,请自行参考，融入到实际中可能需要大家自己做一些思考，比如： 1、文件路径； 2、文件名；
                 * 3、文件格式; 4、文件大小的限制;
                 */
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(
                                file.getOriginalFilename())));
                System.out.println(file.getName());
                System.out.println(file.getOriginalFilename());
                out.write(file.getBytes());
                out.flush();
                out.close();
                R report = roomService.report(userInfo, file.getOriginalFilename(), seatId, timeInterval);
                return report.getMsg();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }

        } else {
            return "上传失败，因为文件是空的.";
        }
    }


    /**
     * 回到座位
     * @param file
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/reportBack")
    public R reportBack(@RequestParam("file") MultipartFile file,
                                   @LoginUser UserInfo userInfo) {
        if (!file.isEmpty()) {
            try {
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(
                                file.getOriginalFilename())));
                out.write(file.getBytes());
                out.flush();
                out.close();
                return roomService.reportBack(userInfo, file.getOriginalFilename());
            } catch (Exception e) {
                return R.fail("上传失败");
            }
        } else {
            return R.fail("上传失败");
        }
    }
}
